package com.smedi.ismedi.authoritymanagement.core.port.adapter.service.impl;

import com.smedi.ismedi.authoritymanagement.core.port.adapter.service.OrganizationRemoteApiService;
import com.smedi.ismedi.organization.api.dto.response.HrOrganizationResponse;
import com.smedi.ismedi.organization.api.dto.response.ProjectOrganizationResponse;
import com.smedi.ismedi.organization.api.feign.RemoteHrOrganizationFeignApi;
import com.smedi.ismedi.organization.api.feign.RemoteProjectOrganizationFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: xulinjun
 * @Date: 2021-6-10 20:47
 */
@Slf4j
@Service
@AllArgsConstructor
public class OrganizationRemoteApiServiceImpl implements OrganizationRemoteApiService {
    private final RemoteHrOrganizationFeignApi remoteHrOrganizationFeignApi;

    private final RemoteProjectOrganizationFeignApi remoteProjectOrganizationFeignApi;

    @Override
    public Boolean checkHrOrganizationAvailable(Long hrOrganizationId) {
        ResponseData<Boolean> response = remoteHrOrganizationFeignApi.getCheckAvailable(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public HrOrganizationResponse getHrOrganizationView(Long hrOrganizationId) {
        ResponseData<HrOrganizationResponse> response = remoteHrOrganizationFeignApi.getHrOrganizationView(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public ProjectOrganizationResponse getProjectOrganizationView(Long projectOrganizationId) {
        ResponseData<ProjectOrganizationResponse> response = remoteProjectOrganizationFeignApi.getProjectOrganizationView(projectOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<Long> getHrOrganizationChildrenOrgIdList(Long hrOrganizationId) {
        ResponseData<List<Long>> response = remoteHrOrganizationFeignApi.getHrOrganizationChildrenOrgIdList(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<Long> getProjectOrganizationChildrenOrgIdList(Long projectOrganizationId) {
        ResponseData<List<Long>> response = remoteProjectOrganizationFeignApi.getProjectOrganizationChildrenProjectOrganizationIdList(projectOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public Boolean checkProjectOrganizationAvailable(Long hrOrganizationId) {
        ResponseData<Boolean> response = remoteProjectOrganizationFeignApi.getCheckAvailable(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public Map<Long, String> getHrOrganizationChildrenOrgMap(Long hrOrganizationId) {
        ResponseData<Map<Long, String>> response = remoteHrOrganizationFeignApi.getHrOrganizationChildrenOrgMap(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public Map<Long, String> getProjectOrganizationChildrenMap(Long hrOrganizationId) {
        ResponseData<Map<Long, String>> response = remoteProjectOrganizationFeignApi.getProjectOrganizationChildrenMap(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public HrOrganizationResponse getTopHrOrganizationScopeList(List<Long> assignmentHrOrganizationIds, boolean showAll) {
        ResponseData<HrOrganizationResponse> response = remoteHrOrganizationFeignApi.getTopHrOrganizationScopeList(assignmentHrOrganizationIds, showAll);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public HrOrganizationResponse getHrOrganizationRoot() {
        ResponseData<HrOrganizationResponse> response = remoteHrOrganizationFeignApi.getHrOrganizationRoot();
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<ProjectOrganizationResponse> getAllProjectOrganizationsByProjectId(Long projectId) {
        ResponseData<List<ProjectOrganizationResponse>> response = remoteProjectOrganizationFeignApi.getQueryProjectOrganizationListByProjectId(projectId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<Long> getParentHrOrganizationIdsByHrOrganizationId(Long hrOrganizationId) {
        ResponseData<List<Long>> response = remoteHrOrganizationFeignApi.getParentHrOrganizationIdsByHrOrganizationId(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<Long> getAllHrOrganizationIdsWhenYouAreInCompanyLevel(List<Long> internalAssignmentHrOrganizationIds) {
        ResponseData<List<Long>> response = remoteHrOrganizationFeignApi.getAllHrOrganizationIdsWhenYouAreInCompanyLevel(internalAssignmentHrOrganizationIds);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<HrOrganizationResponse> getAllHrOrganization() {
        ResponseData<List<HrOrganizationResponse>> response = remoteHrOrganizationFeignApi.getAllHrOrganizationList();
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<HrOrganizationResponse> getQueryHrOrganizationList(List<Long> hrOrganizationIds) {
        ResponseData<List<HrOrganizationResponse>> response = remoteHrOrganizationFeignApi.getQueryHrOrganizationList(hrOrganizationIds);
        if(!response.isSuccess()){
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR,response.getApplication(),String.valueOf(response.getErrorMsg()));
        }
        return response.getData();
    }
}
